package com.moonshineproject.atomic.multiplayer
{
	public class PsuedoTime
	{
		
		private static var instance:PsuedoTime;
		
		private var averagedOffset:int = 0;
		
		public var mp:CocomoComponents;
		
		public static function getInstance():PsuedoTime {
			if (!instance)
				instance = new PsuedoTime();
			
			return instance;
		}

		public function measurementDone(t:TimeMeasurement, serverTimestamp:int):void {
			var now:int = int(new Date());
			var then:int = t.time;
			
			var roundtrip:int = now - then;
			var unicornTime:int = int( roundtrip/2 );
			
			serverTimestamp -= unicornTime;
			
			//then vs. serverTimestamp
			if (then > serverTimestamp)
				averagedOffset = then - serverTimestamp;
			else
				averagedOffset = serverTimestamp - then;
		}
		
		public function getPsuedoTimestamp():int {
			return getLocalTimestamp() - averagedOffset;			
		}

		private function getLocalTimestamp():int {
			
			return int(new Date());
			
		}

	}
}